// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/semantic/semantic.min.css'
import './assets/public.less'
import VueResource from 'vue-resource'
import AV from './global.js';

Vue.config.productionTip = false
Vue.use(VueResource);
// Vue.http.options.emulateJSON = true;
Vue.mixin({
	methods:{
		getData(cb){
			this.project = AV.Object.createWithoutData('Projects', this.$route.query.id);
			var query = new AV.Query('Projects');
			query.get(this.$route.query.id).then((pro) => {
				this.title.name = pro.attributes.name;
				this.common = JSON.parse(pro.attributes.common);
			})
			// 查询type
			function findType(project){
				return new Promise((resolve) => {
					var type = new AV.Query('Types');
					type.equalTo('targetProject', project);
					type.find().then((response) => {
						resolve(response)
					})
				})
			}
			function findSubtype(type){
				return new Promise((resolve) => {
					var subtype = new AV.Query('Subtypes');
					subtype.equalTo('targetType', type);
					subtype.find().then((response) => {
						resolve(response)
					})
				})
			}
			function findApi(subtype){
				return new Promise((resolve) => {
					var api = new AV.Query('Apis');
					api.equalTo('targetSubtype', subtype);
					api.find().then((response) => {
						resolve(response)
					})
				})
			}
			findType(this.project).then((types) => {
				var typeList = [];
				types.forEach((type) => {
					var subtypeList = [], subtypeApiList = [];
					findSubtype(type).then((subtypes) => {
						subtypes.forEach((subtype) => {
							var apiList = [];
							findApi(subtype).then((apis) => {
								apis.forEach((api) => {
									apiList.push({
										id: api.id,
										name: api.attributes.name,
										editing: api.attributes.editing,
										apiContent: api.attributes.apiContent
									})
								})
								this.$nextTick(()=>{
									if(cb) cb();
								})
							})
							// 让二级分类里的api部分永远排在目录的后面
							subtypeList.push({
								id: subtype.id,
								name: subtype.attributes.name,
								isApi: subtype.attributes.isApi,
								editing: subtype.attributes.editing,
								collapse: false,
								content: apiList,
								apiContent: subtype.attributes.apiContent
							})
						})
					})
					typeList.push({
						id: type.id,
						name: type.attributes.name,
						editing: type.attributes.editing,
						collapse: false,
						content: subtypeList
					})
				})
				this.list = typeList
			})
		}
	}
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
